// Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "opencv2/core.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <vector>

#include "PaddleUtil.h"
#include <include/args.h>

using namespace PaddleOCR;

shared_ptr<PaddleUtil> PaddleUtil::m_self;

void PaddleUtil::init()
{
    if (!m_self) {
        FLAGS_det = false;
        FLAGS_rec = true;
        FLAGS_cls = false;
        FLAGS_rec_model_dir = "C:\\Users\\HUAWEI\\.paddleocr\\whl\\rec\\en\\en_"
                              "PP-OCRv4_rec_infer";
        m_self = make_shared<PaddleUtil>();
    }
}

void PaddleUtil::rec_image(const string& imageFile)
{

    std::vector<cv::String> cv_all_img_names;
    cv_all_img_names.push_back(imageFile);

    std::vector<std::vector<OCRPredictResult>> ocr_results =
        ocr.ocr(cv_all_img_names, FLAGS_det, FLAGS_rec, FLAGS_cls);

    for (int i = 0; i < cv_all_img_names.size(); ++i) {
        if (FLAGS_benchmark) {
            cout << cv_all_img_names[i] << '\t';
            for (int n = 0; n < ocr_results[i].size(); n++) {
                for (int m = 0; m < ocr_results[i][n].box.size(); m++) {
                    cout << ocr_results[i][n].box[m][0] << ' '
                         << ocr_results[i][n].box[m][1] << ' ';
                }
            }
            cout << endl;
        } else {
            cout << cv_all_img_names[i] << "\n";
            Utility::print_result(ocr_results[i]);
            if (FLAGS_visualize && FLAGS_det) {
                cv::Mat srcimg =
                    cv::imread(cv_all_img_names[i], cv::IMREAD_COLOR);
                if (!srcimg.data) {
                    std::cerr << "[ERROR] image read failed! image path: "
                              << cv_all_img_names[i] << endl;
                    exit(1);
                }
                std::string file_name = Utility::basename(cv_all_img_names[i]);

                Utility::VisualizeBboxes(
                    srcimg, ocr_results[i], FLAGS_output + "/" + file_name
                );
            }
            cout << "***************************" << endl;
        }
    }
}
